{% extends "subpage/base.html" %}
{% block title %}
    网络安全部
{% endblock %}
{% block scripts %}
    {{ super() }}
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/ueditor.config.js') }}"></script>
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/ueditor.all.min.js') }}"></script>
    <script type="text/javascript"
            src="{{ url_for("static",filename="extra/My97DatePicker/WdatePicker.js") }}"></script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/lang/zh-cn/zh-cn.js') }}"></script>
    <script src="{{ url_for('static',filename='ueditor/ueditor.parse.js') }}"></script>
    <script>
        setTimeout(function () {
            uParse('div',
                {
                    'highlightJsUrl': '{{ url_for("static",filename="ueditor/third-party/SyntaxHighlighter/shCore.js") }}',
                    'highlightCssUrl': '{{ url_for("static",filename="ueditor/third-party/SyntaxHighlighter/shCoreDefault.css") }}'

                })
        }, 300);
    </script>
{% endblock %}
{% block table %}
    <div class="input-items clearfix">

        <div class="form-group clearfix">
            <div class="col-xs-6">
                <label for="inputEvent" class="col-xs-4 control-label"><i class="require">*</i>事件</label>
                <div class="col-xs-8">
                    <input type="text" class="form-control" id="inputEvent" placeholder="事件"/>
                </div>
            </div>
            <div class="col-xs-6">
                <label for="inputType" class="col-xs-4 control-label"><i class="require">*</i>类型</label>
                <div class="col-xs-8">
                    <input type="text" class="form-control" id="inputType" placeholder="类型"/>
                </div>
            </div>
        </div>
        <div class="form-group clearfix">
            <label for="inputDesc" class="col-xs-2 control-label" style="padding-left: 30px"><i class="require">*</i>说明</label>
            <div class="col-xs-10 inputDesc">
                <input type="text" class="form-control" id="inputDesc" placeholder="说明"/>
            </div>
        </div>

        <div class="form-group clearfix">
            <div class="col-xs-6">
                <label for="timespan" class="control-label col-xs-4"><i class="require">*</i>时间</label>
                <div class="col-xs-8">
                    <input type="text" id="timespan" class="Wdate"
                           onclick="WdatePicker({maxDate:'%y-%M-%d 23:59',dateFmt:'yyyy/MM/dd HH:mm',alwaysUseStartDate:true})"/>
                </div>
            </div>
            <div class="col-xs-6">
                <button class="btn btn-primary operate-add-btn" style="margin-left: 16px">添加</button>
            </div>
        </div>
    </div>
    <div class="show-input-items clearfix">
        <table class="table table-bordered">
            <tbody>
            <tr>
                <th>事件</th>
                <th>类型</th>
                <th>时间</th>
                <th>说明</th>
                <th>操作</th>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            </tbody>
        </table>
    </div>
{% endblock %}
{% block ueditorbox %}
    <script id="editor2" type="text/plain"></script>
    <div class="submit-box clearfix">
        <button type="button" class="btn btn-primary btn-submit-confirm pull-left">提交</button>
        <button type="button" class="btn btn-default pull-right cancel-return">取消</button>
    </div>
{% endblock %}
{% block time_field %}
    <div class="time_field">
        <label for="dutyTime" class="control-label col-xs-4" style="padding-top: 6px;padding-left: 0"><i
                class="require">*</i>日报时间</label>
        <div class="col-xs-8">
            <input type="text" id="dutyTime" class="Wdate"
                   onclick="WdatePicker({maxDate:'%y-%M-%d',dateFmt:'yyyy/MM/dd',alwaysUseStartDate:true})"/>
        </div>
    </div>
{% endblock %}
{% block content %}
    {{ super() }}
    <script>
        $(function () {
            var index = 0, trs = $('.table tbody tr').length, total = 0, title = GetQueryString('departAlias'),
                depart_id = GetQueryString('departId'), dutyId = GetQueryString("dutyId"), originalData,
                deleteArray = [], addArray = [];
            var ue1 = UE.getEditor('editor2', {
                serverUrl: "/upload",
                initialFrameWidth: 880,  //初始化编辑器宽度,默认1000
                initialFrameHeight: 500
            });
            if (!dutyId) {
                ue1.ready(function () {
                    //查询是否已存在模板
                    ajaxRequest("GET", GLOBAL_VARIABLE.BASE_URL + "/gettemplate", {"departId": depart_id}, success, error, "{{ csrf_token }}");
                    function success(data) {
                        if (data.code === 0) {
                            ue1.setContent(data.data.tempText);
                        } else if (data.code === 1) {
                            $.myAlert({title: '出错提示', message: data.errmessage});
                        }
                    }

                    function error() {
                        $.myAlert({title: '出错提示', message: '请求出错！'});
                    }
                });
            } else if (dutyId) {
                ajaxRequest("GET", GLOBAL_VARIABLE.BASE_URL + "/datashow", {
                    "dutyId": GetQueryString("dutyId"),
                    "departId": GetQueryString("departId"),
                    "level": 2
                }, function (data) {
                    originalData = data.tableList;
                    $("#dutyTime").val(data.richTextTime);
                    ue1.ready(function () {
                        ue1.setContent(data.richText);
                    });
                    //给表格赋值
                    toTableVal(originalData);
                }, function (error) {
                    $.myAlert({title: "出错提示", message: "请求出错！"});
                });
            }
            //给表格赋值
            function toTableVal(list) {
                console.log(list);
                if (list.length <= trs - 1) {
                    for (var i = 0; i < list.length; i++, index++, total++) {
                        var eventId = list[i].event_id, eventTitle = list[i].title, eventType = list[i].type,
                            eventTime = list[i].ctime, eventContent = list[i].content;
                        $('.table tbody tr').eq(i + 1).attr({
                            "data-eventid": eventId
                        }).html('<td>' + eventTitle + '</td><td>' + eventType + '</td><td>' + eventTime + '</td><td>' + eventContent + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td>');
                    }
                } else {
                    for (var j = 0; j < list.length; j++, index++, total++) {
                        var eventId = list[j].event_id, eventTitle = list[j].title, eventType = list[j].type,
                            eventTime = list[j].ctime, eventContent = list[j].content;
                        if (index < trs - 1) {
                            $('.table tbody tr').eq(j + 1).attr({
                                "data-eventid": eventId
                            }).html('<td>' + eventTitle + '</td><td>' + eventType + '</td><td>' + eventTime + '</td><td>' + eventContent + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td>');
                        } else {
                            $('.table tbody').append('<tr data-eventid="' + eventId + '"><td>' + eventTitle + '</td><td>' + eventType + '</td><td>' + eventTime + '</td><td>' + eventContent + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td></tr>');
                        }
                    }
                }
            }

            var titleStr = GetQueryString("departAlias") + "&nbsp;&nbsp;&nbsp;二线值班报告";//如果编辑状态的话应该放在ajax success回调的方法下
            $('.title-text').html(titleStr);
            $(document).on('click', '.operate-add-btn', function () {
                var inputEvent = $('#inputEvent').val(), inputType = $('#inputType').val(),
                    inputDesc = $('#inputDesc').val(), timespan = $('#timespan').val();
                if (inputEvent && inputEvent.length > 0 && inputType && inputType.length > 0 && inputDesc && inputDesc.length > 0 && !inputEvent.match(/^[ ]+$/) && !inputEvent.match(/^[ ]+$/) && !inputDesc.match(/^[ ]+$/) && timespan.length > 0) {
                    if (index < trs - 1) {
                        //清空数据
                        $('#inputEvent').val('');
                        $('#inputType').val('');
                        $('#inputDesc').val('');
                        $('#timespan').val('');
                        var change_trs = $(".table tr").length;//可变的表格条数
                        if (change_trs - 1 > index) {
                            $('.table tbody tr').eq(index + 1).html('<td>' + inputEvent + '</td><td>' + inputType + '</td><td>' + timespan + '</td><td>' + inputDesc + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td>');
                        } else {
                            $('.table tbody').append('<tr><td>' + inputEvent + '</td><td>' + inputType + '</td><td>' + timespan + '</td><td>' + inputDesc + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td></tr>');
                        }
                        index++;
                        total++;
                    } else {
                        //清空数据
                        $('#inputEvent').val('');
                        $('#inputType').val('');
                        $('#inputDesc').val('');
                        $('#timespan').val('');
                        var str = '<tr><td>' + inputEvent + '</td><td>' + inputType + '</td><td>' + timespan + '</td><td>' + inputDesc + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td></tr>';
                        $('.table tbody').append(str);
                        total++;
                    }
                } else {
                    $.myAlert({title: "错误信息", message: "参数请提交完整!"});
                }

            });
            //执行提交操作
            $('.btn-submit-confirm').click(function () {
                var departId = GetQueryString('departId'), departName = GetQueryString('departName'), data = {},
                    datalist = [], dutyTime = $("#dutyTime").val();
                //获取表格数据
                var table = $('.table tbody tr');
                for (var i = 0; i < total; i++) {
                    datalist.push({
                        title: $(table[i + 1]).find('td').eq(0).html(),
                        type: $(table[i + 1]).find('td').eq(1).html(),
                        time: $(table[i + 1]).find('td').eq(2).html(),
                        content: $(table[i + 1]).find('td').eq(3).html()
                    });
                    if (!$(table[i + 1]).attr("data-eventid")) {
                        addArray.push({
                            title: $(table[i + 1]).find('td').eq(0).html(),
                            type: $(table[i + 1]).find('td').eq(1).html(),
                            time: $(table[i + 1]).find('td').eq(2).html(),
                            content: $(table[i + 1]).find('td').eq(3).html()
                        });
                    }
                }
                //判断是否日报时间为空
                if (!dutyTime || dutyTime.length === 0) {
                    $.myAlert({title: ' 出错提示', message: '请填写日报时间！'});
                    return false;
                }
                //获取富文本编辑器内容
                var u_content = ue1.getContent();
                //判断传的值是否全为空
                if (u_content.length === 0) {
                    $.myAlert({title: '出错提示', message: '请完善富文本框内容！'});
                    return;
                }
                if (dutyId) {//修改
                    data = {
                        "type": "edit",
                        "dutyId": dutyId,//全局变量，地址栏传参
                        "departId": departId,
                        "datalist": {
                            plus: addArray,
                            delete: deleteArray
                        },
                        "departName": departName,
                        "substance": u_content,
                        "level": 2,
                        "richTextTime": dutyTime
                    }
                } else {//创建
                    data = {
                        "type": "create",
                        "departId": departId,
                        "datalist": datalist,
                        "departName": departName,
                        "substance": u_content,
                        "level": 2,
                        "richTextTime": dutyTime
                    };
                }

                function success(data) {
                    if (data.code === 0) {//提交成功
                        var dutyId = GetQueryString("dutyId") || data.dutyId, duty_time = data.richTextTime;
                        window.location.href = '/views?dutyId=' + dutyId + '&departId=' + departId + '&departAlias=' + title + '&duty_time=' + duty_time;
                    } else if (data.code === 1) {
                        $.myAlert({title: '出错提示', message: data.errmessage});
                    }
                }

                function error(err) {
                    $.myAlert({title: '出错提示', message: err.errmessage});
                }

                ajaxRequest('POST', GLOBAL_VARIABLE.BASE_URL + '/datasave', JSON.stringify(data), success, error, '{{ csrf_token }}');
            });
            $(document).on("click", ".remove-item", function () {
                var cur_parentobj = $(this).closest("tr"), eventId = cur_parentobj.attr("data-eventid");
                if (dutyId && eventId) {
                    deleteArray.push(parseInt(eventId));
                }
                cur_parentobj.remove();
                //当前表格中数据减少一条
                index--;
                total--;
            });
            $(document).on("click", ".cancel-return", function () {
                window.history.go(-1);
            });
        });
    </script>
{% endblock %}